package com.uyayo.framework.pagehelper;


import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 分页结果包装类
 * uyayo-framework-pagehelper
 * Created by hfj on 2017/2/2.
 * page 从1开始计数
 */
@ApiModel(value = "分页结果包装类")
public class PageWrapper<T> implements Serializable {

    private static final long serialVersionUID = -5178865693670317791L;
    @ApiModelProperty("总记录数")
    private long total;

    @ApiModelProperty("分页参数类")
    private Pageable pageable;

    @ApiModelProperty("内容列表")
    private List<T> content = new ArrayList<>();

    public int offset() {
        return (pageable.getCurrentPage() * pageable.getPageSize()) - pageable.getPageSize();
    }


    public PageWrapper() {
    }

    public PageWrapper(List<T> content, Pageable pageable, long total) {
        this.content.addAll(content);
        this.pageable = pageable;
        this.total = !content.isEmpty() && pageable != null && offset() + pageable.getPageSize() > total
                ? offset() + content.size() : total;
    }

    public PageWrapper(List<T> content) {
        this(content, null, null == content ? 0 : content.size());
    }


    public int getTotalPages() {
        return getSize() == 0 ? 1 : (int) Math.ceil((double) total / (double) getSize());
    }


    public long getTotalElements() {
        return total;
    }


    public int getNumber() {
        return pageable == null ? 1 : pageable.getCurrentPage();
    }


    public int getSize() {
        return pageable == null ? 0 : pageable.getPageSize();
    }


    public int getNumberOfElements() {
        return content.size();
    }


    public List<T> getContent() {
        return Collections.unmodifiableList(content);
    }

    public void setContent(List<T> content) {
        this.content = content;
    }

    public boolean hasContent() {
        return !content.isEmpty();
    }

    public boolean isFirst() {
        return !hasPrevious();
    }

    public boolean isLast() {
        return !hasNext();
    }

    public boolean hasNext() {
        return getNumber() + 1 < getTotalPages();
    }

    public boolean hasPrevious() {
        return getNumber() > 1;
    }

    public long getTotal() {
        return total;
    }

    public void setTotal(long total) {
        this.total = total;
    }

    public Pageable getPageable() {
        return pageable;
    }

    public void setPageable(Pageable pageable) {
        this.pageable = pageable;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof PageWrapper)) return false;
        PageWrapper<?> page = (PageWrapper<?>) o;
        return getTotal() == page.getTotal() &&
                Objects.equals(getPageable(), page.getPageable()) &&
                Objects.equals(getContent(), page.getContent());
    }

    @Override
    public int hashCode() {
        return Objects.hash(getTotal(), getPageable(), getContent());
    }

    @Override
    public String toString() {
        final StringBuilder sb = new StringBuilder("PageWrapper{");
        sb.append("total=").append(total);
        sb.append(", pageable=").append(pageable);
        sb.append(", content=").append(content);
        sb.append('}');
        return sb.toString();
    }


}
